/*
 *所有关于Tb_Project_Fundtemplate_Item类的业务代码应在此处编写
*可使用repository.调用常用方法，获取EF/Dapper等信息
*如果需要事务请使用repository.DbContextBeginTransaction
*也可使用DBServerProvider.手动获取数据库相关信息
*用户信息、权限、角色等使用UserContext.Current操作
*Tb_Project_Fundtemplate_ItemService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter
*/
using MYData.Entity.DomainModels;
using MYData.Core.Utilities;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using MYData.System.IRepositories;
using MYData.Core.EFDbContext;
using System.Threading.Tasks;
using MYData.Entity.DTO.ApiEntity.Input;
using Mapster;
using System;

namespace MYData.System.Services
{
    public partial class Tb_Project_Fundtemplate_ItemService
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly ITb_Project_Fundtemplate_ItemRepository _repository;//访问数据库
        private readonly VOLContext _context;

        [ActivatorUtilitiesConstructor]
        public Tb_Project_Fundtemplate_ItemService(
            ITb_Project_Fundtemplate_ItemRepository dbRepository,
            IHttpContextAccessor httpContextAccessor
,
            VOLContext context)
        : base(dbRepository)
        {
            _httpContextAccessor = httpContextAccessor;
            _repository = dbRepository;
            _context = context;
            //多租户会用到这init代码，其他情况可以不用
            //base.Init(dbRepository);
        }

        WebResponseContent webResponse = new WebResponseContent();

        /// <summary>
        /// 添加预算科目
        /// </summary>
        /// <param name="inputDto"></param>
        /// <returns></returns>
        public async Task<WebResponseContent> InsertTemplateItemAsync(FundingItemInput_Dto inputDto)
        {
            var model = inputDto.Adapt<Tb_Project_Fundtemplate_Item>();
            _repository.AddAsync(model);
            return webResponse.OK("添加预算科目成功！",await _repository.SaveChangesAsync());
        }

        /// <summary>
        /// 删除项目经费预算模板科目(逻辑删除)
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task<WebResponseContent> SoftDeleteFundingTemplateItemAsync(string Id)
        {
            
            var model = _context.Set<Tb_Project_Fundtemplate_Item>().Find(Id);          
            model.isDelete = 1;
            _repository.Update(model);
            return webResponse.OK("删除成功!", await _repository.SaveChangesAsync());
        }

        /// <summary>
        /// 删除项目经费模板预算科目(物理删除)
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task<WebResponseContent> DeleteFundingTemplateItemAsync(string Id)
        {
            var model = _context.Set<Tb_Project_Fundtemplate_Item>().Find(Id);
            _repository.Delete(model);
            return webResponse.OK("删除成功!", await _repository.SaveChangesAsync());
        }

        /// <summary>
        /// 修改项目经费预算科目
        /// </summary>
        /// <returns></returns>
        public async Task<WebResponseContent> UpdateFundingTemplateItemAsync(UpdatefundingItem_Dto inputDto)
        {
            var Entity = inputDto.Adapt<Tb_Project_Fundtemplate_Item>();
            _repository.Update(Entity);
            return webResponse.OK("修改科目成功!", await _repository.SaveChangesAsync());
        }
    }
}
